//弹窗
;(function ($) {
    var defaults = {
        level:1, //pop level
        type: 1, //pop type (ps:not pop style)
        desc:"", 
        callback: function(){}
    };

    var Panel = function(el,options){
        this.$el = $(el);
        this.opts = $.extend( {}, defaults, options) ;
    };
    Panel.prototype = {
        init: function(){
            var _this = this;
            var pn = this.$el
            if(this.opts.type == 1){
                if ($('.mask').length < 1) { 
                    $('body').append("<div class='mask animating'></div>");
                    $('.mask').on("touchmove",function(e){
                        e.preventDefault();
                        e.stopPropagation()
                    })
                }
                var mLayer = $('.mask');
                var close = pn.find(".jsClose");

                if(_this.opts.level == 1){
                    close.off("close").on("close",function(){
                        $.fn.popPanel.hide(pn,mLayer)
                    })
                    $.fn.popPanel.show(pn,mLayer)
                }else if(_this.opts.level == 2){
                    close.off("close").on("close",function(){
                        $.fn.popPanel.hide(pn)
                    })
                    $.fn.popPanel.show(pn)
                }
                close.off("click").on("click",function(e){
                    e.preventDefault();
                    e.stopPropagation()
                    $(this).trigger("close")
                })

                mLayer.off("click").on("click",function(e){
                    e.preventDefault();
                    //e.stopPropagation()
                    $.fn.popPanel.hide($(".pop-panel"),mLayer)
                })
    
            }else if(this.opts.type == 2) {
                //type:2 自动隐藏弹层
                var desc = this.opts.desc
                pn.html(desc);
                $.fn.popPanel.show(pn)

                var timer = setTimeout(function () {
                    $.fn.popPanel.hide(pn)
                }, 2500);
            }else{
                console.log("弹窗类型错误")
            }
            
            this.opts.callback()
        }
    };
    
    $.fn.popPanel = function (options) {
        return this.each(function(){
            var pop = new Panel(this, options)
            pop.init()
        });

    }
    $.fn.popPanel.show = function($el,mask) {
        $('body').css({ overflowY: 'hidden' });
        if(mask){
            mask.addClass("active on")
        }
        $el.addClass("active pop-out");
    }

    $.fn.popPanel.hide = function($el,mask) {
        $("body").css({ overflowY: "auto" })
        if(mask){
            mask.removeClass("active on")
        }
        $el.removeClass("active pop-out");
    }

    $.fn.popPanel.return = function($el) {
        $el.removeClass("active pop-out");
    }

})(jQuery);

/*默认值*/
Object.extend = function (destination, source) {
    if (!destination) return source;
    for (var property in source) {
        if (!destination[property]) {
            destination[property] = source[property];
        }
    }
    return destination;
}
//添加css3样式
var classNames = ['Webkit', 'ms', 'Moz', 'O', ''];
$.fn.addClass3 = function (name, value) {
    var o = this[0];
    var cName = name.charAt(0).toUpperCase() + name.substring(1);
    for (var i = 0; i < classNames.length; i++) {
        o.style[classNames[i] + cName] = value;
    }
    return $(o);
}
//触摸屏事件
$.fn.touches = function (options) {
    var setting = {
        init: null,//初始化
        touchstart: null,  //按下
        touchmove: null, //滑动
        touchend: null //抬起
    };
    options = Object.extend(options, setting);
    var $this = this, touchesDiv = $this[0];
    if (!$this[0]) return;
    touchesDiv.addEventListener('touchstart', function (ev) {
        options.touchstart && options.touchstart.call($this, ev);

        function fnMove(ev) {
            options.touchmove && options.touchmove.call($this, ev);
        }

        function fnEnd(ev) {
            options.touchend && options.touchend.call($this, ev);
            document.removeEventListener('touchmove', fnMove, false);
            document.removeEventListener('touchend', fnEnd, false);
        }
        document.addEventListener('touchmove', fnMove, false);
        document.addEventListener('touchend', fnEnd, false);
        return false;
    }, false)
    options.init && options.init.call($this);
}

//屏幕旋转完成事件(横屏:horizontal|竖屏:vertical)
$.rotateEnd = function (fn) {
    function toResize() {
        var winW = document.documentElement.clientWidth,
        winH = document.documentElement.clientHeight;
        setTimeout(function () { fn && fn(winW > winH ? "horizontal" : "vertical"); }, 200);
    }
    $(window).resize(toResize).trigger('resize');
};

$(document).ready(function () {
    //$("#GBcity-panel").popPanel({level:2})
    

    //固定菜单，滚动监听
    function fixedBar() {
        var sTop = $(window).scrollTop();
        var _top = $(".hd-wp").offset().top;
        if (sTop > _top) {
            $(".header").addClass("fixedBar");
        } else {
            $(".header").removeClass("fixedBar");
        }
    }

    var isiPhone = /iphone/i.test(navigator.userAgent.toLowerCase());
    var isiPad = /ipad/i.test(navigator.userAgent.toLowerCase());
    var isfirefox = /firefox/i.test(navigator.userAgent.toLowerCase());

    if (isiPad || isiPhone || (isfirefox && isiPhone)) {
        $(".hd-wp").addClass("sticky")
    } else {
        if ($(".hd-wp").length > 0) {
            $(window).scroll(function () {
                fixedBar()
            });
        }
    }

});